package com.julis.activity.record.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;
import com.julis.R;
import com.julis.activity.setting.user.User_Activity;
import com.julis.bean.Life_UserInfo;
import com.julis.db.Z_UserInfoDao;
import com.julis.ui.Dialog.MultiSelectDialog;
import com.julis.ui.Dialog.MultiSelectInterface;
import com.util.JSONUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by juli on 2020/3/3.
 */

public class MyRVUserAdapter extends RecyclerView.Adapter<MyRVUserAdapter.MyTVHolder> {
    /**
     * 数据存储集合
     **/
    public List<Life_UserInfo> mDataList = new ArrayList<Life_UserInfo>();

    /**
     * 集合方式添加数据
     *
     * @param collection 集合
     */
    public boolean addItem(Collection<Life_UserInfo> collection) {
        boolean b = mDataList.addAll(collection);
        notifyDataSetChanged();
        return b;

    }

    User_Activity user_fragment;

    public void addfragment(User_Activity user_activity) {
        this.user_fragment = user_activity;
    }

    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    Z_UserInfoDao z_userInfoDao;

    public MyRVUserAdapter(Context context, Z_UserInfoDao z_userInfoDao) {
        this.mLayoutInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.z_userInfoDao = z_userInfoDao;

    }

    List<String> ccList = new ArrayList<String>();

    /**
     * 清空数据
     */
    public void clear() {
        mDataList.clear();
    }

    @Override
    public MyRVUserAdapter.MyTVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyRVUserAdapter.MyTVHolder(mLayoutInflater.inflate(R.layout.tab_item_1_userlist, parent, false));
    }

    @Override
    public void onBindViewHolder(final MyRVUserAdapter.MyTVHolder holder, int pos) {
        StringBuilder stringBuilder = new StringBuilder();
        String Account = mDataList.get(pos).getAccount();
        String Password = mDataList.get(pos).getPassword();
        String Permission = mDataList.get(pos).getPermission();
        Log.e("zzz1", Permission);

        String value = Permission.substring(1, Permission.length() - 1);
        String[] split = value.split(",");
        for (int i = 0; i < split.length; i++) {

            if (split[i].contains("true")) {
                String substring = split[i].substring(1, split[i].indexOf(":") - 1);
                stringBuilder.append(substring);
                stringBuilder.append(",");
            }
        }

//        Log.e("zz", "xx" + Permission);
        ccList.clear();


        holder.tv_zhanghao.setText("账号:" + Account);
//        holder.tv_mima.setText("密码:" + Password);
        String vaulename = stringBuilder.toString();

//        if (vaulename.length() > 0) {
//            holder.tv_quanxian.setText("权限:" + vaulename.substring(0, vaulename.length() - 1));
//        }else {
//            holder.tv_quanxian.setText("权限:"  );
//        }
        stringBuilder.delete(0, stringBuilder.length());
//        holder.xiugaiquanxian.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                Toast.makeText(mContext, "修改权限", Toast.LENGTH_SHORT).show();
//                String id = mDataList.get(pos).getAccount();
//                HashMap<String, Object> stringStringMap = JSONUtils.ObjectTomap(Permission);
//                multiSelect(stringStringMap,id);
//
//            }
//        });

        holder.shanchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(mContext, "删除", Toast.LENGTH_SHORT).show();
                String id = mDataList.get(pos).getAccount();
                showshanchu(id);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    class MyTVHolder extends RecyclerView.ViewHolder {
        TextView tv_zhanghao;
        RoundButton shanchu;

        MyTVHolder(View itemView) {
            super(itemView);
            tv_zhanghao = (TextView) itemView.findViewById(R.id.tv_zhanghao);
            shanchu = (RoundButton) itemView.findViewById(R.id.shanchu);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private MyRVUserAdapter.OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(MyRVUserAdapter.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void showshanchu(final String s) {
        new AlertDialog.Builder(mContext).setMessage("您确定要删除此账号?")
                .setCancelable(false)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                z_userInfoDao.detelone(s);
                user_fragment.shuaxin();
                Toast.makeText(mContext, "账号删除成功！", Toast.LENGTH_SHORT).show();
            }
        }).show();

    }

    HashMap<String, Object> maps;

    //MultiSelect 多选列表
    public void multiSelect(HashMap<String, Object> newSelect, String id) {
        listData.clear();
        initSelectData(newSelect);

        new MultiSelectDialog(mContext, listData, new MultiSelectInterface() {
            @Override
            public void sure(ArrayList<HashMap<String, Object>> newSelect, int[] selectId) {
                listData = newSelect;
                maps = new HashMap<String, Object>();
                Log.e("zzz", Arrays.toString(selectId));
                for (int i = 0; i < newSelect.size(); i++) {
                    HashMap<String, Object> stringObjectHashMap = newSelect.get(i);

                    String text = (String) stringObjectHashMap.get("text");
                    maps.put(text, stringObjectHashMap.get("isSelect"));
                }
                String s = JSONUtils.mapToJson(maps);
//                Log.e("zzz", s);
                z_userInfoDao.updateUserper(id, s);
                user_fragment.shuaxin();
                Toast.makeText(mContext, "权限修改成功！", Toast.LENGTH_SHORT).show();
            }
        });
    }

    ArrayList listData = new ArrayList<HashMap<String, Object>>();


    //多选列表初始数据
    private void initSelectData(HashMap<String, Object> map) {


        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();

        int i = 0;
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            HashMap<String, Object> maps = new HashMap<String, Object>();
//            Log.e("zz", "xx" + entry.getKey());
            maps.put("position", i);
            maps.put("text", entry.getKey());
            maps.put("isSelect", entry.getValue());
            i++;
            listData.add(maps);
        }

    }


}
